﻿using Dapper;
using Infrastructure.DataBaseConnect;
using Infrastructure.IRepository;
using Model.BusinessModel.OutPut;
using Model.DomainModel;
using Model.Flow;
using System.Data;
using Utility.Enum;

namespace Infrastructure.Repository
{
    /// <summary>
    /// 流程节点配置仓储接口
    /// </summary>
    public class FlowNodeConfigRepository : Repository<FlowNodeConfig>, IFlowNodeConfigRepository
    {
        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="flowNodeConfig">审核节点数据</param>
        public void BulkInsert(List<FlowNodeConfig> flowNodeConfig)
        {
            string sql = "  insert into FlowNodeConfig (Id,FlowId,FormId,FlowNodeId,NodeType,ExpireConfig,IsExpireRemind,FieldAuthConfig,CreateTime,CreateUserKey) values(@Id,@FlowId,@FormId,@FlowNodeId,@NodeType,@ExpireConfig,@IsExpireRemind,@FieldAuthConfig,@CreateTime,@CreateUserKey) ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Execute(sql, flowNodeConfig);
        }

        /// <summary>
        /// 获取流程节点配置
        /// </summary>
        /// <param name="flowId">流程id</param>
        /// <param name="flowNodeId">节点id</param>
        /// <returns></returns>
        public FlowNodeConfig? GetExpireDate(string flowId, string flowNodeId)
        {
            string sql = "  select ExpireConfig,FieldAuthConfig,IsExpireRemind from  FlowNodeConfig where FlowId=@FlowId and FlowNodeId=@FlowNodeId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.QueryFirstOrDefault<FlowNodeConfig>(sql, new { FlowId = flowId, FlowNodeId = flowNodeId });
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="flowNodeConfigs">删除数据</param>
        public void BulkDeleteByFlowId(List<FlowNodeConfig> flowNodeConfigs)
        {
            string sql = " delete from FlowNodeConfig where FlowId=@FlowId and FlowNodeId=@FlowNodeId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Execute(sql, flowNodeConfigs);
        }

        /// <summary>
        /// 根据条件过期数据
        /// </summary>
        /// <param name="isExpireRemind">是否存在过期配置</param>
        /// <returns></returns>
        public List<FlowNodeExpireDataOutPut> GetExpireConfig(bool isExpireRemind)
        {
            string sql = @"   select a.FlowId,a.FlowNodeId,a.FormId,a.ExpireConfig,b.FlowContent,b.FlowApplyState,b.ApplyTitle,b.FlowApplyId,b.CurrentNodeUpdateTime,b.CurrentNodeId,b.CurrentNodeName,b.CurrentNodeReviewerId,b.CurrentNodeReviewerName from  FlowNodeConfig as a
                             inner join FlowApply as b on a.FlowId = b.FlowId  and a.FlowNodeId=b.CurrentNodeId
                              where IsExpireRemind=@IsExpireRemind and b.FlowApplyState |@FlowApplyState=@FlowApplyState ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<FlowNodeExpireDataOutPut>(sql, new { IsExpireRemind = isExpireRemind, FlowApplyState = (int)FlowApplyStateEnum.InExamine + (int)FlowApplyStateEnum.AuditDismissal }).ToList();
        }
    }
}
